home *** CD-ROM | disk | FTP | other *** search
- unit Mainmenu;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Menus, inifiles, buttons, dbtables, DB,
- filectrl,
- utils, {misc support}
- dbutils, {openDB, etc.}
- importDD, {AddToDict}
- about,
- ddedDlg, {edit data dictionary}
- gentable, {generate tables from data dictionary}
- createdd, {Make a new dictionary}
- BrowseDD, {Browse the data dictionary}
- {dbctrl, {set up tfields}
- {scaler, {user controled scale form utility}
- runinfo, ExtCtrls, Grids; {Heap & DB info display}
-
- type
- DDValidationtype = (IsValidDD, DoesNotExist, ExistbutnotDD, NewDD, EmptyString );
- TMain = class(TForm)
- DictTable: TTable;
- DictDataSource: TDataSource;
- DictQuery: TQuery;
- SourceDatabase: TDatabase;
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- Edit1: TMenuItem;
- Edit2: TMenuItem;
- Browse1: TMenuItem;
- About1: TMenuItem;
- Options: TMenuItem;
- NewDD1: TMenuItem;
- OpenDD1: TMenuItem;
- N1: TMenuItem;
- Exit1: TMenuItem;
- Label1: TLabel;
- M_status: TMemo;
- DDEditbox: TEdit;
- B_DDlist: TBitBtn;
- DDListBox: TListBox;
- FontDialog1: TFontDialog;
- AboutDDict1: TMenuItem;
- RunInfo1: TMenuItem;
- OpenDialog: TOpenDialog;
- DefineexistingDB1: TMenuItem;
- Help1: TMenuItem;
- Printdictionary1: TMenuItem;
- N2: TMenuItem;
- Generateunittocreatedatabase1: TMenuItem;
- Createemptydatabase1: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- Designatetargetdirectory1: TMenuItem;
- Label5: TLabel;
- Labele: TLabel;
- Label8: TLabel;
- L_update: TLabel;
- L_size: TLabel;
- L_numrecs: TLabel;
- Label4: TLabel;
- L_numTables: TLabel;
- L_curTable: TLabel;
- targetDatabase: TDatabase;
- TargetTable: TTable;
- TargetDataSource: TDataSource;
- TargetQuery: TQuery;
- LB_allFields: TListBox;
- L_allfields: TLabel;
- LB_curFields: TListBox;
- LB_tables: TListBox;
- SG_Summary: TStringGrid;
- procedure FormCreate(Sender: TObject);
- function CheckOutDD(Sender: Tobject; whichone : integer) : DDValidationType;
- procedure SetUpAlias(sender: Tobject; whichone : integer);
- procedure Edit2Click(Sender: TObject);
- procedure NewDD1Click(Sender: TObject);
- function MakeNewDD(sender: TObject; const filespec : string) : boolean;
- procedure SelectDD(Sender: TObject);
- procedure B_DDlistClick(Sender: TObject);
- procedure DDListBoxClick(Sender: TObject);
- procedure DDEditboxKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure DDListBoxExit(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure DDEditboxDblClick(Sender: TObject);
- procedure FontchangeClick(Sender: TObject);
- procedure RunInfo1Click(Sender: TObject);
- procedure OpenDD1Click(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure Browse1Click(Sender: TObject);
- procedure AboutDDict1Click(Sender: TObject);
- {procedure Resize1Click(Sender: TObject);}
- procedure Button1Click(Sender: TObject);
- procedure DefineexistingDB1Click(Sender: TObject);
- procedure Designatetargetdirectory1Click(Sender: TObject);
- procedure Createemptydatabase1Click(Sender: TObject);
- procedure LB_tablesClick(Sender: TObject);
- procedure LB_curFieldsClick(Sender: TObject);
- procedure LB_allFieldsClick(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- procedure Printdictionary1Click(Sender: TObject);
- procedure Generateunittocreatedatabase1Click(Sender: TObject);
- procedure Help1Click(Sender: TObject);
- private
- FValidDD : DDValidationType; {flag on status of current contents of DDeditbox}
- FIniFile : TIniFile;
- procedure initDDeditStuff; {initialize with no ini file}
- procedure NotImplemented(const what: string);
- public
- { DataDictCtrl : tDDCtrl;}
- FCtrlDictName : string; {name of data dictionary that controls DB in this app}
- NewDDName,
- DDPathName,
- DDTableName,
- TargetPathName : string;
- EditThisField : boolean;
- GoToTable,
- GoToField : string;
- DictWasChanged : boolean;
- Procedure ReadIniFile;
- Procedure ChangeIniFile;
- end;
-
- var
- Main: TMain;
-
- implementation
- uses
- mystrng
- {$ifdef buggy}
- , toolhelp;
- {$else}
- ;
- {$endif}
-
-
- {$R *.DFM}
-
- {========================= Main form routines ===========================}
-
- procedure Tmain.initDDeditStuff;
- begin
- l_size.caption := '';
- l_update.caption := '';
- l_numtables.caption := '';
- {l_numfields.caption := '';}
- l_numrecs.caption := '';
- ddEditBox.text := 'No Dictionary active.';
- lb_tables.items.clear;
- FCtrlDictName := '';
- DictWasChanged := false;
- end;
-
-
- procedure TMain.FormCreate(Sender: TObject);
- begin
- { M_checkout.lines.clear;}
- SourceDatabase.close;
- SourceDatabase.Params.Clear;
- {$IFDEF buggy}
- tstlst := tstringlist.create;
- {$ENDIF}
- scaleform(self);
- ReadIniFile;
- If DDeditBox.text = ''
- then initDDeditStuff
- else setUpAlias(sender, 0);
- { DataDictCtrl := tDDctrl.create(self);}
- end;
-
- procedure TMain.FormDestroy(Sender: TObject);
- begin
- ChangeIniFile;
- {$IFDEF buggy}
- tstlst.free;
- {$ENDIF}
- end;
-
- Procedure TMain.ReadIniFile;
- var tmpstr : string;
- begin
- FIniFile := TiniFile.Create(appname+'.ini');
- FiniFile.ReadSection('DDFiles', DDListBox.items);
- if DDListBox.items.count = 0
- then DDEditBox.text := ''
- else DDEditBox.text := DDListBox.items[0];
- FCtrlDictName := FiniFile.ReadString('CtrlDict', 'current', appname+'.dbf');
- FiniFile.free;
- end;
-
- Procedure TMain.ChangeIniFile;
- var i : integer;
- begin
- FIniFile := TiniFile.Create(appname+'.ini');
- FiniFile.eraseSection('DDFiles');
- for i := 0 to DDListBox.items.count -1 do
- FiniFile.writeString('DDFiles', DDListBox.items[i], '1');
- FiniFile.free;
- end;
-
- {======================= Data Dictionary selection routines ===============}
-
- function Tmain.CheckOutDD(sender: tObject; whichone : integer): DDValidationtype;
- var
- tablefound : boolean;
- sqlstr,
- thistable : string;
- pathname : dirstr;
- tablenum,
- numtables,
- numFields : integer;
- FileInfo : TsearchRec;
- tableField : tField;
-
- function FieldInfo: string;
- begin
- end;
-
- begin
- result := isValidDD;
- numtables := 0; numFields := 0;
- l_size.caption := '';
- l_update.caption := '';
- l_numtables.caption := '';
- {l_numfields.caption := '';}
- l_numrecs.caption := '';
- lb_tables.items.clear;
- lb_curFields.items.clear;
- lb_allFields.items.clear;
- if fileExists(DDListBox.items[whichone])
- then begin
- FindFirst(DDListBox.items[whichone], faAnyfile, fileinfo);
- L_update.caption := datetimetostr(fileDateToDateTime(Fileinfo.time));
- l_size.caption := IntTostr(FileInfo.size);
- {not total size, should also get size of .dbt }
- end
- else begin
- cursor := crDefault;
- result := DoesNotExist;
- m_status.hide;
- exit;
- end;
- DictCtrl.OpenDictionary(DDEditBox.text, SourceDataBase, DictTAble, DictQuery, DictDataSource);
- SG_summary.rowcount := DictCtrl.FDBSG.rowcount +1 ; {extra row for field names}
- SG_summary.colcount := DictCtrl.FDBSG.colcount;
- for numFields := ord(ddfTable_name) to ord(ddfformula) do
- SG_summary.cells[numFields,0] := DictTableFieldNames[ddfOffsets(NumFields)];
- for numfields := ord(ddfTable_name) to ord(ddfformula) do
- SG_summary.rows[numfields+1] := DictCtrl.FDBSG.rows[numfields];
-
- {This section could be eliminated since the section above grabs all the
- relevant information; but if eliminated, l_numrecs, etc need to be
- set from this info}
- if openDB(SourceDataBase, DictTAble, DictQuery, DictDataSource,
- DDPathName, DDTableName)
- then begin
- l_numrecs.caption := intToStr(dictTable.RecordCount);
- sqlstr := 'SELECT * FROM '+DDTableName;
- Dictquery.sql.add(sqlstr);
- Dictquery.prepare;
- Dictquery.open;
- Dictquery.first;
- { get tablenames in data dictionary, stick in M_tableList lines}
- if DictQuery.findfield('TABLE_NAME') = nil
- then begin
- cursor := crDefault;
- MessageDlg(DDListBox.items[whichone]+#13+'is not a Data Dictionary Database.', mtInformation, [mbOK], 0);
- m_status.hide;
- result := ExistButNotDD;
- exit;
- end;
- lb_tables.items.add(DictQuery.findfield('TABLE_NAME').text); {get first one}
- lb_allfields.items.add(fieldSummary(DictQuery));
- inc(numfields);
- DictQuery.next;
- while not DictQuery.eof do begin
- tablefound := false;
- thistable := DictQuery.findfield('TABLE_NAME').text;
- lb_allfields.items.add(fieldSummary(DictQuery));
- inc(numFields);
- for tablenum := 0 to lb_tables.items.count - 1 do
- if lb_tables.items[tablenum] = thistable
- then begin
- tablefound := true;
- break;
- end;
- {done looking for thistable}
- if not tablefound
- then lb_tables.items.add(thistable);
- DictQuery.next;
- end; {while searching for table names}
- l_numtables.caption := intToSTr(lb_tables.items.count);
- {l_numfields.caption := IntToStr(numfields);}
- l_allfields.caption := 'All '+IntToStr(numfields)+' fields';
- { thistable := MinimizeName(DDEditBox.text, Canvas,
- GroupBox1.width-canvas.TextWidth('Dictionary: '));
- groupbox2.caption := 'Dictionary: '+thistable;
- }
- DictQuery.close;
- Sourcedatabase.close;
-
- end
- else begin
- l_size.caption := '';
- l_update.caption := '';
- m_status.hide;
- result := ExistbutnotDD;
- end;
- end;
-
-
- (**
- try
- SourceDataBase.close;
- SourceDatabase.Params.clear;
- SourceDatabase.Params.Add('PATH='+DDPathName);
- SourceDatabase.open;
- DictTable.DatabaseName:= SourceDataBase.databasename;
- DictTable.tablename := DDTableName;
- DictTable.Active:= True;
- l_numrecs.caption := intToStr(dictTable.RecordCount);
- DictDataSource.DataSet:= DictTable;
- DictQuery.databaseName := SourceDataBase.databasename;
- Dictquery.dataSource := DictDataSource;
- Dictquery.close;
- Dictquery.sql.clear;
- Dictquery.params.clear;
- sqlstr := 'SELECT * FROM '+DDTableName;
- Dictquery.sql.add(sqlstr);
- Dictquery.prepare;
- Dictquery.open;
- Dictquery.first;
- { get tablenames in data dictionary, stick in M_tableList lines}
- if DictQuery.findfield('TABLE_NAME') = nil
- then begin
- cursor := crDefault;
- MessageDlg(DDListBox.items[whichone]+#13+'is not a Data Dictionary Database.', mtInformation, [mbOK], 0);
- m_status.hide;
- result := ExistButNotDD;
- exit;
- end;
- lb_tables.items.add(DictQuery.findfield('TABLE_NAME').text); {get first one}
- inc(numfields);
- DictQuery.next;
- while not DictQuery.eof do begin
- tablefound := false;
- thistable := DictQuery.findfield('TABLE_NAME').text;
- inc(numFields);
- for tablenum := 0 to lb_tables.items.count - 1 do
- if lb_tables.items[tablenum] = thistable
- then begin
- tablefound := true;
- break;
- end;
- {done looking for thistable}
- if not tablefound
- then lb_tables.items.add(thistable);
- DictQuery.next;
- end; {while searching for table names}
- except
- on EdataBaseError do begin
- cursor := crDefault;
- MessageDlg('Not a data base file or other DB error', mtInformation, [mbOK], 0);
- l_size.caption := '';
- l_update.caption := '';
- m_status.hide;
- result := ExistbutnotDD;
- end;
- end; {of exceptions}
- l_numtables.caption := intToSTr(lb_tables.items.count);
- l_numfields.caption := IntToStr(numfields);
- thistable := MinimizeName(DDEditBox.text, Canvas,
- GroupBox1.width-canvas.TextWidth('Dictionary: '));
- groupbox2.caption := 'Dictionary: '+thistable;
- DictQuery.close;
- Sourcedatabase.close;
- end;
- **)
-
- Procedure Tmain.SetUpAlias(sender : tObject; whichone : integer);
- begin
- DDPathName := extractFilePath(DDListBox.items[whichone]);
- DDTableName := extractFileName(DDListBox.items[whichone]);
- DDTableName := copy(DDTableName, 1, pos('.', DDtableName)-1);
- m_status.lines.clear;
- m_status.show;
- m_status.lines.add('Checking out Dictionary:');
- m_status.lines.add('Path: '+DDPathName);
- m_status.lines.add('Table: '+DDTableName);
- m_status.update;
- cursor := crHourglass;
- FValidDD := CheckoutDD(sender, whichone);
- m_status.hide;
- cursor := crDefault;
- case FValidDD of
- IsValidDD : begin
- if whichone <> 0
- then DDListBox.items.exchange(0, whichone);
- end;
- DoesNotExist : begin
- l_size.caption := '';
- l_update.caption := '';
- if messagedlg(DDListBox.items[whichone]+#13+
- 'This data dictionary does not exist.'+#13+
- ' Click OK to create new, empty DD.',
- mtConfirmation, [mbOK,mbCancel], 0) = mrOk
- then
- if not makenewDD(sender, DDListBox.items[whichone])
- then DDListBox.items.delete(whichone);
- end;
- ExistButNotDD : begin
- DDListBox.items.delete(whichone);
- DDEditBox.text := '';
- end;
- end;
- DDListBox.hide;
- DictWasChanged := false;
- end;
-
-
- procedure TMain.Edit2Click(Sender: TObject);
- begin
- if FvalidDD = IsValidDD
- then DDmultpagedlg.show;
- if DictWasChanged
- then selectDD(sender);
- end;
-
- procedure TMain.NewDD1Click(Sender: TObject);
- begin
- with openDialog do begin
- Options := [ofPathMustExist] - [ofFileMustExist];
- title := 'Enter name of new dictionary';
- end;
- if OpenDialog.Execute
- then begin
- If fileExists(OpenDialog.FileName)
- then begin
- MessageDlg('File exists already', mtInformation, [mbOK], 0);
- exit;
- end
- else begin
- NewDDname := opendialog.filename;
- createDDForm.showmodal;
- if CreateDDForm.modalResult = mrYes
- then begin
- DDEditBox.text := newDDName;
- selectDD(sender);
- end
- else messagedlg('unsuccessful attempt to build new dd',
- mtinformation, [mbOK], 0);
- end;
- end;
- end;
-
- function Tmain.makenewDD(sender: Tobject; const filespec : string): boolean;
- begin
- If fileExists(filespec)
- then begin
- {not needed, this only called when file not found... I hope}
- MessageDlg(Filespec +' exists already', mtInformation, [mbOK], 0);
- result := false;
- end
- else begin
- NewDDname := filespec;
- createDDForm.showmodal;
- if CreateDDForm.modalResult = mrYes
- then begin
- result := true;
- DDEditBox.text := newDDName;
- selectDD(sender);
- end
- else begin
- messagedlg('unsuccessful attempt to build new dd',
- mtinformation, [mbOK], 0);
- result := false;
- end;
- end;
- end;
-
- procedure TMain.SelectDD(Sender: TObject);
- var i : integer;
- found : boolean;
- begin
- with DDListBox do
- begin
- found := false;
- for i := 0 to items.count -1 do
- if DDEditBox.text = items[i]
- then begin
- found := true;
- break;
- end;
- if found
- then setUpAlias(sender, i)
- else begin
- items.add(DDEditBox.text);
- setUpAlias(sender, items.count -1);
- end;
- end;
- end;
-
-
- procedure TMain.B_DDlistClick(Sender: TObject);
- begin
- DDListBox.show;
- end;
-
- procedure TMain.DDListBoxClick(Sender: TObject);
- begin
- DDEditBox.text := DDListBox.items[DDListBox.itemindex];
- DDListBox.hide;
- SelectDD(sender);
- end;
-
- procedure TMain.DDEditboxKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- if key = VK_RETURN
- then SelectDD(Sender);
- end;
-
- procedure TMain.DDListBoxExit(Sender: TObject);
- begin
- DDListBox.hide;
- end;
-
- procedure TMain.DDEditboxDblClick(Sender: TObject);
- begin
- if FValidDD = IsValidDD
- then DDmultpagedlg.show
- else selectDD(sender);
- end;
-
- procedure TMain.OpenDD1Click(Sender: TObject);
- begin
- with openDialog do begin
- filename := '*.dbf';
- options := [ofPathMustExist, ofFileMustExist] ;
- title := 'Select existing dictionary dbf';
- end;
- if OpenDialog.Execute
- then begin
- DDEditBox.text := OpenDialog.FileName;
- SelectDD(sender);
- end;
- end;
-
-
-
-
- {=================== misc ============================}
-
-
- procedure TMain.FontchangeClick(Sender: TObject);
- begin
- FontDialog1.Font := Main.Font;
- if FontDialog1.Execute then
- Main.Font := FontDialog1.Font;
- end;
-
- procedure TMain.RunInfo1Click(Sender: TObject);
- begin
- runinfoform.updateinfo('Main About click');
- runinfoform.show;
- end;
-
-
- procedure TMain.Exit1Click(Sender: TObject);
- begin
- {$IFDEF buggy}
- runinfoform.updateinfo('Main Close');
- runinfoform.show;
- {$ENDIF}
- close;
- end;
-
- procedure TMain.Browse1Click(Sender: TObject);
- begin
- BrowseDDForm.FormActivate(sender);
- end;
-
- procedure TMain.AboutDDict1Click(Sender: TObject);
- begin
- AboutBox.show;
- end;
-
- {procedure TMain.Resize1Click(Sender: TObject);
- begin
- ScalerForm.setWhichForm(main);
- if ScalerForm.showmodal = mrYes
- then update;
- end;}
-
-
- procedure TMain.Button1Click(Sender: TObject);
- begin
- {DataDictCtrl := tDDctrl.create(self);}
- { SourceDataBase.close;
- SourceDatabase.Params.clear;
- SourceDatabase.Params.Add('PATH='+DDPathName);
- SourceDatabase.open;
- DictTable.DatabaseName:= SourceDataBase.databasename;
- DictTable.tablename := DDTableName;
- DictTable.Active:= True;
- DictDataSource.DataSet:= DictTable;
- DictQuery.databaseName := SourceDataBase.databasename;
- Dictquery.dataSource := DictDataSource;
- Dictquery.close;
- dictCtrlForm.show;
- if dictCtrlForm.FillTableDef(DictQuery, DDTableName) = success
- then begin
- setFieldDef(DictTable, 'Element', 'Table_Name');
- end;}
- end;
-
- procedure TMain.DefineexistingDB1Click(Sender: TObject);
- var thisfile : integer;
- begin
- if not (FValidDD = IsValidDD)
- then begin
- messageDlg('Open a dictionary first', mtInformation, [mbOK], 0);
- exit;
- end;
- with opendialog do begin
- options := [ofPathMustExist, ofFileMustExist, ofAllowMultiSelect];
- title := 'Select table(s) to pull into data dictionary';
- end;
- if OpenDialog.Execute
- then begin
- if l_numrecs.caption <> ''
- then
- if MessageDlg('Append to existing records?', mtConfirmation, [mbYes,mbNo], 0) = mrNo
- then exit;
- for thisfile := 0 to OpenDialog.files.count -1 do
- begin
- AddToDict(SourceDatabase, DictTable, DictQuery, DictDataSource,
- TargetDatabase, TargetTable, TargetQuery, TargetDataSource,
- DDPathName, DDTableName,
- ExtractFilePath(OpenDialog.files[thisfile]),
- ExtractFileName(OpenDialog.files[thisfile]));
- SelectDD(sender);
- end;
- end;
- end;
-
-
- procedure TMain.Designatetargetdirectory1Click(Sender: TObject);
- begin
- BuildTableForm.show;
- end;
-
- procedure TMain.Createemptydatabase1Click(Sender: TObject);
- begin
- if checkoutDD(sender, 0) = IsValidDD
- then BuildTableForm.show
- else messageDlg('no valid dictionary...', mtError, [mbOK],0);
- end;
-
- procedure TMain.LB_tablesClick(Sender: TObject);
- var i, count : integer;
- thistable : string;
- begin
- LB_curFields.items.clear;
- thisTable := LB_Tables.items[LB_tables.itemindex];
- count := 0;
- for i := 0 to LB_AllFields.items.count - 1 do
- if get_word(LB_allFields.items[i], 1) = thisTable
- then begin
- LB_curFields.items.add(copy(LB_allFields.items[i], length(thistable)+3,255));
- inc(count);
- end;
- L_curTable.caption := intTostr(count)+' fields in '+thistable;
- end;
-
- procedure TMain.LB_curFieldsClick(Sender: TObject);
- begin
- if FvalidDD = IsValidDD
- then begin
- EditThisField := true;
- goToTable := trim(LB_Tables.items[LB_tables.itemindex]);
- GoToField := trim(get_word(LB_curFields.items[LB_curFields.itemindex],1));
- DDmultpagedlg.show;
- end;
- if DictwasChanged
- then selectDD(sender);
- EditThisField := false;
- end;
-
- procedure TMain.LB_allFieldsClick(Sender: TObject);
- begin
- if FvalidDD = IsValidDD
- then begin
- EditThisField := true;
- goToTable := get_word(LB_allFields.items[LB_allFields.itemindex], 1);
- GoToField := get_word(LB_allFields.items[LB_allFields.itemindex],3);
- DDmultpagedlg.show;
- end;
- EditThisField := false;
- end;
-
- procedure TMain.FormActivate(Sender: TObject);
- begin
- if DictwasChanged
- then selectDD(sender);
- end;
-
- procedure TMain.Printdictionary1Click(Sender: TObject);
- begin
- NotImplemented('Print Dictionary');
- end;
-
- procedure TMain.Generateunittocreatedatabase1Click(Sender: TObject);
- begin
- NotImplemented('Make Pascal unit to Generate table(s)');
- end;
-
- procedure TMain.Help1Click(Sender: TObject);
- begin
- NotImplemented('Help system');
- end;
-
- procedure TMain.NotImplemented(const what: string);
- begin
- MessageDlg(What+#13+'is not implemented yet...', mtInformation, [mbOK],0);
- end;
-
- end.
-